//
//  CZStatusTopView.m
//  XinLang_OC
//
//  Created by WONG on 16/3/11.
//  Copyright © 2016年 cheuhui. All rights reserved.
//

#import "CZStatusTopView.h"
#import "CZUser.h"
#import "CZStatus.h"
#import "NSDate+Sina.h"
#import "NSString+Extension.h"

@interface CZStatusTopView ()
/**
 *  分割视图
 */
@property (nonatomic,strong) UIView *separatorView;
/**
 *  用户头像
 */
@property (nonatomic,strong) UIImageView *iconView;
/**
 *  用户昵称
 */
@property (nonatomic,strong) UILabel *nameLabel;
/**
 *  时间
 */
@property (nonatomic,strong) UILabel *timeLable;
/**
 *  来源
 */
@property (nonatomic,strong) UILabel *sourceLabel;
/**
 *  会员等级图标
 */
@property (nonatomic,strong) UIImageView *mbrankImageView;
/**
 *  认证类型图标
 */
@property (nonatomic,strong) UIImageView *verifiedImageView;


@end

@implementation CZStatusTopView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self prepareUI];
    }
    return self;
}

// 准备UI
- (void)prepareUI {
    // 添加子控件
    [self addSubview:self.separatorView];
    [self addSubview:self.iconView];
    [self addSubview:self.nameLabel];
    [self addSubview:self.timeLable];
    [self addSubview:self.sourceLabel];
    [self addSubview:self.mbrankImageView];
    [self addSubview:self.verifiedImageView];
    
    /*
        添加约束
     */
    [self.separatorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(ScreenWidth, 10));
        make.left.equalTo(self);
        make.top.equalTo(self);
    }];
    
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(35, 35));
        make.left.equalTo(self.separatorView.mas_left).offset(8);
        make.top.equalTo(self.separatorView.mas_bottom).offset(8);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        // nameLabel的left = iconView right偏移8
        make.left.equalTo(self.iconView.mas_right).offset(8);
        // nameLabel的top = iconView的top
        make.top.equalTo(self.iconView);
    }];
    
    [self.mbrankImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(14, 14));
        make.left.equalTo(self.nameLabel.mas_right).offset(8);
        make.top.equalTo(self.nameLabel);
    }];
    
    [self.timeLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconView.mas_right).offset(8);
        make.bottom.equalTo(self.iconView);
    }];
    
    [self.sourceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.timeLable.mas_right).offset(8);
        make.bottom.equalTo(self.timeLable);
    }];
    
    [self.verifiedImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(17, 17));
        make.left.equalTo(self.iconView.mas_right).offset(-8.5);
        make.bottom.equalTo(self.iconView.mas_bottom).offset(8.5);
    }];
}

- (void)setStatus:(CZStatus *)status {
    _status = status;
    
    NSURL *iconUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@",_status.user.profile_image_url]];
    [self.iconView sd_setImageWithURL:iconUrl placeholderImage:[UIImage imageNamed:@"avatar"]];
    
    self.nameLabel.text = _status.user.screen_name;
    
    self.timeLable.text = [[NSDate sinaStringToDate:_status.created_at] sinaDateDescription];
    
    self.sourceLabel.text = _status.source.sinaSource;
    
    self.verifiedImageView.image = _status.user.verifiedImage;
    
    self.mbrankImageView.image = _status.user.mbrankImage;
}



#pragma mark - lazy load

- (UIView *)separatorView {
    if (!_separatorView) {
        _separatorView = [[UIView alloc] init];
        _separatorView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
    }
    return _separatorView;
}

- (UIImageView *)iconView {
    if (!_iconView) {
        _iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"avatar"]];
    }
    return _iconView;
}

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.textColor = [UIColor darkGrayColor];
        _nameLabel.font = [UIFont systemFontOfSize:14];
    }
    return _nameLabel;
}

- (UILabel *)timeLable {
    if (!_timeLable) {
        _timeLable = [[UILabel alloc] init];
        _timeLable.textColor = [UIColor orangeColor];
        _timeLable.font = [UIFont systemFontOfSize:9];
    }
    return _timeLable;
}

- (UILabel *)sourceLabel {
    if (!_sourceLabel) {
        _sourceLabel = [[UILabel alloc] init];
        _sourceLabel.textColor = [UIColor lightGrayColor];
        _sourceLabel.font = [UIFont systemFontOfSize:9];
    }
    return _sourceLabel;
}

- (UIImageView *)mbrankImageView {
    if (!_mbrankImageView) {
        _mbrankImageView = [[UIImageView alloc] init];
        _mbrankImageView.backgroundColor = [UIColor orangeColor];;
    }
    return _mbrankImageView;
}

- (UIImageView *)verifiedImageView {
    if (!_verifiedImageView) {
        _verifiedImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"avatar_vip"]];
    }
    return _verifiedImageView;
}

@end















